<?php
/**
 * Primary Bootstrap class of the application
 */
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	/**
	 * Set the default application timezone
	 *
	 * @return void
	 */
	protected function _initTimezone()
	{
		//$autoloader = Zend_Loader_Autoloader::getInstance();
		//$autoloader->setFallbackAutoloader(true);

		$autoloader = new Zend_Loader_Autoloader_Resource (array (
							'basePath' => APPLICATION_PATH . "/modules",
							'namespace' => 'Application',
		));
		$autoloader -> addResourceType ( 'service','services','Service');
		date_default_timezone_set('Europe/Berlin');
	}

	/**
	 * Set system locale
	 *
	 * @return void
	 */
	protected function _initSystemLocale()
	{
		//$this->bootstrap('locale'); //génère un bug
		//setlocale(LC_CTYPE, $this->getResource('locale')->toString() . '.utf-8');
		
		//$this->bootstrap('locale'); //génère un bug
		//setlocale(LC_CTYPE, $this->getResource('locale')->toString());
	}

	/**
	 * Set the adapter for the datamapper
	 *
	 * @return void
	 */
	protected function _initDataMapper()
	{
		$this->bootstrap('db');
		require_once 'App/Model/Mapper/MapperAbstract.php';
		App_Model_Mapper_MapperAbstract::setDefaultAdapter(
		$this->getPluginResource('db')->getDbAdapter()
		);
	}


	/**
	 * Setup additional view parameters
	 *
	 * @return Zend_View
	 */
	protected function _initViewEnvironment()
	{
		$this->bootstrap('view');

		$view = $this->getPluginResource('view')->getView();

		//TODO : savoir c'est que cela veut dire, et l'adapter
		//$view->headMeta('OvXFW6oQhQx90DlvnS0NNRoGSZl+yeN9khAc8CUiXmg=', 'verify-v1')
		//->headMeta('mailto+http:sha1:0e3e03684e1a67deb89bfe75f21b1749bba7e1f1', 'microid');
	}

	/**
	 * Set HTTP headers
	 *
	 * @return Zend_Controller_Response_Http
	 */
	protected function _initResponse()
	{
		$this->bootstrap('frontController');

		$response = new Zend_Controller_Response_Http();

		$response->setHeader('language', 'en')
		->setHeader('content-language', 'en')
		->setHeader('Content-Type', 'text/html; charset=utf-8');

		$this->getPluginResource('frontController')->getFrontController()
		->setResponse($response);
			
		return $response;
	}

	protected function _initConfig()
	{
		$config = new Zend_Config($this->getOptions());
		Zend_Registry::set('config', $config);
		return $config;
	}

	protected function _initCache()
	{
		$this->bootstrap('db');
		$frontendOptions = array(   'lifetime' => 7200, // temps de vie du cache de 2 heures
								   'automatic_serialization' => true
		);
		$backendOptions = array(    // Répertoire où stocker les fichiers de cache
								    'cache_dir' => APPLICATION_PATH . '/../data/cache'
								    );
								    $cache = Zend_Cache::factory('Output',
                             'File',
								    $frontendOptions,
								    $backendOptions);
								    Zend_Registry::set('cache', $cache);
								    Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
	}

	protected function _initDatabaseRegistry()
	{
		$this->bootstrap("db");
		$db = $this->getResource("db");
		$db->setFetchMode(Zend_Db::FETCH_ASSOC);//TODO : ne sert à rien
		Zend_Registry::set('db', $db);
		return $db;
	}

	/**
	 * Init lang, template, view scripts paths, title, default metadata description,
	 * default metadata keywords
	 * @return unknown_type
	 */
	protected function _initPlugins()
	{
		$this->bootstrap("databaseRegistry");
		require_once 'App/Loader/Template.php';
		$frontController = Zend_Controller_Front::getInstance();
		//$frontController->registerPlugin(new Wmp_Controller_Plugin_ViewSetup(),10);
		//$frontController->registerPlugin(new Wmp_Controller_Plugin_Translations(),30);
		$frontController->registerPlugin(new App_Loader_Template(),50);

		//$frontController->registerPlugin(new Wmp_Controller_Plugin_DevelopmentBar());

		//$frontController->registerPlugin(new Wmp_Controller_Plugin_DebugBar());
		//TODO : mettre le resultat en cache
		//		
		//		
		//
		//
		//
		//
		//		$siteInfo = array();
		//
		//		foreach($sites as $site) {
		//			$siteInfo[$site['name']] = $site;
		//		}

			//print_r($siteInfo);exit;
			//		$layout = Zend_Layout::getMvcInstance();
			//		$desabledLayout = $request->getParam('nolayout');
			//		if($request->isXmlHttpRequest() || (isset($desabledLayout))){
			//			$layout->disableLayout();
				//		}
				//		else {
				//			$layout->setLayoutPath($config['layoutPath']);
				//			if($siteInfo[] !== null){
				//				$layout->setLayout($config['layout']);
				//			}
				//			$configSite = Zend_Registry::get('config');
				//			$layout->setViewSuffix($configSite->layouts->suffix);
				//		}
		}

		protected function _initRoute()
		{
			// setup front controller
			$frontController = Zend_Controller_Front::getInstance();
			//INFO : gerer les routes statics en bdd, lorsqu'on valider/enregistre
			//mettre à  jour un fichier php, que je vais lire pour les mettre ici
			//ou persister pour les manipuler from database, ici avec un foreach
			$router = $frontController->getRouter();

			$defaultNamespace = new Zend_Session_Namespace('Default');

			// Ajout de la nouvelle route
			$router->addRoute(
		    "default",
			new Zend_Controller_Router_Route(
			/*"/:language/:module/:controller/:action/*",*/
		    	"/:language/:site/:module/:controller/:action/*",
			array(
		            "language"      =>  'fr',
		        	"site"        =>  'site',
		            "module"        =>  'default',
		            "controller"    =>  'index',
		            "action"        =>  'index'
		            )
		            )
		            );

		            $route = new Zend_Controller_Router_Route_Static(
            'add-page',
		            array('module'=>'page', 'controller' => 'admin', 'action' => 'add-page')
		            );
		            $router->addRoute('add-page', $route);

		            $route = new Zend_Controller_Router_Route_Static(
            'error',
		            array('module'=>'default', 'controller' => 'error', 'action' => 'error')
		            );
		            $router->addRoute('error',$route);

		            $route = new Zend_Controller_Router_Route_Static(
            'home',
		            array('module'=>'default', 'controller' => 'index', 'action' => 'index')
		            );
		            $router->addRoute('home',$route);
		}

		public function _initHelpers()
		{
			$view = new Zend_View();
			$view->addHelperPath('App/View/Helper/', 'App_View_Helper');
			//->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper'); //INFO: je désactive volontairement ZendX_JQuery
			$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
			$viewRenderer->setView($view);
			Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
			//ZendX_JQuery_View_Helper_JQuery::enableNoConflictMode();
		}

}